<?php


namespace app\admin\controller;

use app\admin\service\PageService;
use app\BaseController;
use think\App;
use think\facade\View;
use think\response\Json;

class Page extends BaseController
{
    protected $service;

    /**
     * Category constructor.
     * @param App $app
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->service = app()->make(PageService::class);
    }

    /**
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(): string
    {
        $list = $this->service->queryListByPage(array());
        $data['list'] = $list;
        $data['total'] = $list->total();
        $data['currentPage'] = $list->currentPage();
        return View::fetch('index', $data);
    }

    public function add(): string
    {
        return View::fetch('add');
    }

    /**
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit(): string
    {
        $id = $this->request->get('id');
        $data['detail'] = $this->service->queryDetail($id);
        return View::fetch('edit', $data);
    }

    public function save(): Json
    {
        $data = $this->request->post();
        $aid = $this->service->save($data);
        return success($aid);
    }

    public function delete(): Json
    {
        return success($this->service->delete(input("id")));
    }
}